﻿using System;
using System.Data;
using System.Data.OracleClient;
using Kangaroo.Model;
using NUnit.Framework;

namespace Kangaroo.DAL.Oracle.Tests
{
    public class DalTestHelper
    {
        public static long CreateSignal(string devId, int devChn, string equipId, byte equipPhase,
            DateTime pdTime, float pdMean, float pdMax, float pdPhase, float pdRep, string pdType,
            string pdTypeProb, string pdLoc, string pdLevel, string pdFile, byte[] prpsGraph)
        {
            var keyParameter = new OracleParameter("key", OracleType.Number) { Direction = ParameterDirection.Output };
            OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.StoredProcedure, "CRUD_PD.CreatePD",
                new OracleParameter("devId", devId),
                new OracleParameter("devChn", devChn),
                new OracleParameter("equipId", equipId),
                new OracleParameter("equipPhase", equipPhase),
                new OracleParameter("pdTime", pdTime),
                new OracleParameter("pdMean", pdMean),
                new OracleParameter("pdMax", pdMax),
                new OracleParameter("pdPhase", pdPhase),
                new OracleParameter("pdRep", pdRep),
                new OracleParameter("pdType", pdType),
                new OracleParameter("pdTypeProb", pdTypeProb),
                new OracleParameter("pdLoc", pdLoc),
                new OracleParameter("pdLevel", pdLevel),
                new OracleParameter("pdFile", pdFile),
                new OracleParameter("prpsGraph", prpsGraph),
                keyParameter);
            var createdKey = (long)((decimal)keyParameter.Value);
            Assert.That(createdKey, Is.GreaterThan(0));
            return createdKey;
        }

        public static int CreateChannel(string devId, short devChn, string equipId, byte equipPhase)
        {
            var keyParameter = new OracleParameter("pkey", OracleType.Number) { Direction = ParameterDirection.Output };
            OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.StoredProcedure, "CRUD_Channel.CreateChannel",
                new OracleParameter("devId", devId),
                new OracleParameter("devChn", devChn),
                new OracleParameter("equipId", equipId),
                new OracleParameter("equipPhase", equipPhase),
                keyParameter);
            var createdKey = (int)((decimal)keyParameter.Value);
            Assert.That(createdKey, Is.GreaterThan(0));
            return createdKey;
        }

        private static long CreateAlarm(string devId, short devChn, string equipId, byte equipPhase,
            DateTime alarmTime, string alarmType, byte alarmLevel, float alarmValue)
        {
            var keyParameter = new OracleParameter("key", OracleType.Number) { Direction = ParameterDirection.Output };
            OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.StoredProcedure, "CRUD_Alarm.CreateAlarm",
                new OracleParameter("devId", devId),
                new OracleParameter("devChn", devChn),
                new OracleParameter("equipId", equipId),
                new OracleParameter("equipPhase", equipPhase),
                new OracleParameter("alarmTime", alarmTime),
                new OracleParameter("alarmType", alarmType),
                new OracleParameter("alarmLevel", alarmLevel),
                new OracleParameter("alarmValue", alarmValue),
                keyParameter);
            var createdKey = (long)((decimal)keyParameter.Value);

            Assert.That(createdKey, Is.GreaterThan(0));
            return createdKey;
        }

        public static long CreateAlarm(AlarmInfo alarmInfo)
        {
            return CreateAlarm(
                alarmInfo.Channel.DevId,
                alarmInfo.Channel.DevChn,
                alarmInfo.Channel.EquipId,
                alarmInfo.Channel.EquipPhase,
                alarmInfo.AlarmTime,
                alarmInfo.AlarmType,
                alarmInfo.AlarmLevel,
                alarmInfo.AlarmValue);
        }

        public static void AssertAlarmEqual(AlarmInfo alarm1, AlarmInfo alarm2)
        {
            Assert.NotNull(alarm1);
            Assert.NotNull(alarm2);
            AssertChannelEqual(alarm1.Channel, alarm2.Channel);
            Assert.That(alarm1.AlarmTime, Is.EqualTo(alarm2.AlarmTime).Within(TimeSpan.FromMilliseconds(10)));
            Assert.That(alarm1.AlarmType, Is.EqualTo(alarm2.AlarmType));
            Assert.That(alarm1.AlarmLevel, Is.EqualTo(alarm2.AlarmLevel));
            Assert.That(alarm1.AlarmValue, Is.EqualTo(alarm2.AlarmValue).Within(0.001));
        }

        public static void AssertChannelEqual(ChannelInfo channel1, ChannelInfo channel2)
        {
            Assert.AreEqual(channel1.DevId, channel2.DevId);
            Assert.AreEqual(channel1.DevChn, channel2.DevChn);
            Assert.AreEqual(channel1.EquipId, channel2.EquipId);
            Assert.AreEqual(channel1.EquipPhase, channel2.EquipPhase);
        }
    }
}
